/**
 * \file Reference.h
 * \brief Reference counting pointer implementation
 * \author Corey Tabaka
 *
 * Reference counting pointer implementation.
 *
 * NOTE: This is different from the kernel's reference counting pointer imp. in
 * that it makes use of dynamic_cast to safely up convert.
 *
 * CAUTION: This code is highly sensitive to the order of operations in
 * order to correctly handle self references (objects that have references
 * to themselves). Every statement is designed in a specific order to
 * prevent destructor loops and multiple deallocations.
 */

/*
   Copyright 2007 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <atomic.h>

#ifndef __USER_REFERENCE_H__
#define __USER_REFERENCE_H__

template<class T>
struct Ref {
	T* const ptr;
	volatile int count;
	volatile int valid;

	Ref(T* p, int c=1) : ptr(p), count(c), valid(1) {}
	
	~Ref() {
		if (valid) {
			atomic_set(0, &valid);
			delete ptr;
		}
	}
	
	Ref* increment() { atomic_add(1, &count); return this; }
	bool decrement() { atomic_add(-1, &count); return count<=0; }
	
	private:
	Ref(const Ref&);
	Ref& operator=(const Ref&);
};

template<class T>
class RefCount {
	public:
	
	Ref<T> *ref;
	const bool self;
	
	explicit RefCount(T* p = 0, bool s=false)
		: ref(p ? new Ref<T>(p, s ? 0 : 1) : 0), self(s) {}
	
	RefCount(const RefCount<T>& other)
		: ref(other.ref ? other.ref->increment() : 0), self(false) {}
	
	template<class U>
	RefCount(const RefCount<U>& other) : self(false) {
		if (other.ref) {
			T* const test = dynamic_cast<T*>(other.ref->ptr);
			ref = reinterpret_cast<Ref<T> *>(test ? other.ref->increment() : 0);
		} else {
			ref = 0;
		}
	}
	
	~RefCount() {
		if (!self && ref && ref->decrement()) {
			delete ref;
		}
		
		ref = 0;
	}
	
	/*template<class U>
	RefCount<T>& operator=(const RefCount<U>& other) {
		if (other.ref != ref) {
			T* const test = dynamic_cast<T*>(other.ptr);
			Ref<T>* tmp = reinterpret_cast<Ref<T> *>(
				(other.ref && test) ? other.ref->increment() : 0);
			
			if (!self && ref && ref->decrement()) {
				delete ref;
			}
			ref = tmp;
		}
		return *this;
	}*/
	
	RefCount<T>& operator=(const RefCount<T>& other) {
		if (other.ref != ref) {
			Ref<T>* tmp = other.ref ? other.ref->increment() : 0;
			
			if (!self && ref && ref->decrement()) {
				delete ref;
			}
			ref = tmp;
		}
		return *this;
	}

	operator T* () const {
		return ref ? ref->ptr : 0;
	}

	T* operator->() const {
		return ref ? ref->ptr : 0;
	}

	T* get() const {
		return ref ? ref->ptr : 0;
	}

	/*template<class newType> operator RefCount<newType>() {
		RefCount<newType> rc(0);
		rc.ref = reinterpret_cast< Ref<newType>* >(
			ref ? ref->increment() : 0);
		
		return rc;
	}*/
};

#define DEF_REF_TYPE(type) \
	typedef	RefCount<type> Ref##type; \
	extern	Ref##type null##type; \
	extern	type * const null##type##Ptr \

#define DEF_NULL_REF(type) \
	type * const null##type##Ptr = 0; \
	Ref##type null##type(null##type##Ptr) \

#endif
